package xyz.hzxiao.utils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * test
 *
 * @author ：FuXx
 * @className : com.usedcar.utils.Test
 * @since ：2023-08-25
 */
public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // supplyAsync();
        System.out.println(2 << 2);
    }

    private static void chained() {
        // CompletableFuture默认线程池为守护线程池，主线程关闭后whenComplete回调会失败
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        try {
            CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
                System.out.println("开始计算，5秒后出计算结果");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return 1;
            }, executorService).whenComplete((v, e) -> {
                if (e == null) {
                    System.out.println("计算完成，update value:" + v);
                }
            }).exceptionally((throwable) -> {
                throwable.printStackTrace();
                return null;
            });
            System.out.println(completableFuture.join());
        } finally {
            executorService.shutdown();
        }
    }

    private static void supplyAsync() {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "Hello world";
        });
        System.out.println(completableFuture.join());
    }

    private static void runAsync() throws InterruptedException, ExecutionException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println(completableFuture.get());
    }

    private static void extracted() throws InterruptedException, ExecutionException {
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + "\t run task");
            TimeUnit.SECONDS.sleep(5);
            return "Hello futureTask";
        });

        new Thread(futureTask, "t1").start();

        System.out.println(Thread.currentThread().getName() + "\t run over");

        System.out.println("result :" + futureTask.get());
    }
}
